package logger

import (
	"log"
	"os"
	"strings"
)

// LogLevel 日志级别
type LogLevel int

const (
	DEBUG LogLevel = iota
	INFO
	WARN
	ERROR
)

var (
	currentLevel LogLevel = INFO
	logger       *log.Logger
)

// Init 初始化日志
func Init(level string) {
	logger = log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile)

	switch strings.ToLower(level) {
	case "debug":
		currentLevel = DEBUG
	case "info":
		currentLevel = INFO
	case "warn":
		currentLevel = WARN
	case "error":
		currentLevel = ERROR
	default:
		currentLevel = INFO
	}
}

// Debug 调试日志
func Debug(v ...interface{}) {
	if currentLevel <= DEBUG {
		logger.SetPrefix("[DEBUG] ")
		logger.Println(v...)
	}
}

// Debugf 格式化调试日志
func Debugf(format string, v ...interface{}) {
	if currentLevel <= DEBUG {
		logger.SetPrefix("[DEBUG] ")
		logger.Printf(format, v...)
	}
}

// Info 信息日志
func Info(v ...interface{}) {
	if currentLevel <= INFO {
		logger.SetPrefix("[INFO] ")
		logger.Println(v...)
	}
}

// Infof 格式化信息日志
func Infof(format string, v ...interface{}) {
	if currentLevel <= INFO {
		logger.SetPrefix("[INFO] ")
		logger.Printf(format, v...)
	}
}

// Warn 警告日志
func Warn(v ...interface{}) {
	if currentLevel <= WARN {
		logger.SetPrefix("[WARN] ")
		logger.Println(v...)
	}
}

// Warnf 格式化警告日志
func Warnf(format string, v ...interface{}) {
	if currentLevel <= WARN {
		logger.SetPrefix("[WARN] ")
		logger.Printf(format, v...)
	}
}

// Error 错误日志
func Error(v ...interface{}) {
	if currentLevel <= ERROR {
		logger.SetPrefix("[ERROR] ")
		logger.Println(v...)
	}
}

// Errorf 格式化错误日志
func Errorf(format string, v ...interface{}) {
	if currentLevel <= ERROR {
		logger.SetPrefix("[ERROR] ")
		logger.Printf(format, v...)
	}
}
